<template>
    <div>
        <div id="baseInfo">
            <div class="qheader" :style="{borderBottom: '0px solid rgb(210,210,210)'}">
                <p>提单人信息</p>
            </div>
            <div class="gcontent">
                <Form :model="applyinfo.user" :label-width="100" label-position="left" class="form form_left">
                    <Row>
                        <Col span="8" v-if="reqId != null && reqId != ''">
                            <FormItem label="工单ID：">
                                <Input v-model="reqId" :readonly="true" />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span="8">
                            <FormItem label="申请人姓名：">
                                <Input v-model="applyinfo.user.user_name" :readonly="true" />
                            </FormItem>
                        </Col>
                        <Col span="8" class="col_left">
                            <FormItem label="申请人单位：">
                                <Input v-model="applyinfo.user.department" :readonly="true" />
                            </FormItem>
                        </Col>
                    </Row>
                </Form>
            </div>
        </div>
        <div id="project">
            <div class="qheader" :style="{borderBottom: '0px solid rgb(210,210,210)'}">
                <p>项目信息</p>
            </div>
            <div class="gcontent">
                <Form ref="projectInfo" :model="applyinfo" :rules="ruleLine" :label-width="100" label-position="left" class="form form_left">
                    <Row>
                        <Col span="8">
                            <FormItem label="工单标题：" prop="reqSubject">
                                <Input v-model="applyinfo.reqSubject" :readonly="ifedit || isStep6" placeholder="XX租户申请资源缩容"/>
                            </FormItem>
                        </Col>
                        <Col span="8" class="col_left">
                            <FormItem label="缩容类型：" :required="true">
                                <Input v-if="ifedit || isStep6" :value="applyinfo.isPartShrink===1?'资源/工具缩容':'租户销户'" :readonly="ifedit" placeholder="XX租户申请资源缩容"/>
                                <Select v-else v-model="applyinfo.isPartShrink" :disabled="ifedit">
                                    <Option :value="i.value" v-for="(i,index) in isPartShrink" :key="index">{{i.label}}</Option>
                                </Select>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row style="color: #bbb;padding-bottom: 10px" v-if="!ifedit">
                        (注：仅项目经理可以选择租户和项目，来进行提单！)
                    </Row>
                    <Row>
                        <Col span="8">
                            <FormItem label="提单租户：" prop="roleId">
                                <Input v-if="ifedit || isStep6" v-model="applyinfo.project.tenant_name" :readonly="true" />
                                <Select v-else v-model="applyinfo.roleId" @on-change="checkRole" :disabled="ifedit">
                                    <Option :value="i.id" v-for="(i,index) in tenantList" :key="index">{{i.tenant_name}}</Option>
                                </Select>
                            </FormItem>
                        </Col>
                        <Col span="8" class="col_left">
                            <FormItem label="关联项目：" :prop="!ifedit?'projectId':''">
                                <Input v-if="ifedit || isStep6" v-model="applyinfo.project.project" :readonly="true" />
                                <Select v-else v-model="applyinfo.projectId" @on-change="setProject($event,0)"  :disabled="ifedit">
                                     <Option :value="item.id" v-for="(item,index) in projectList" :key="index">{{item.project}}</Option>
                                </Select>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span="8" >
                            <FormItem label="运营人员：">
                                <Input v-if="applyinfo.project.business!=null && applyinfo.project.business!=undefined && applyinfo.project.business!=''" v-model="applyinfo.project.business" :readonly="true" />
                                <Input v-else v-model="applyinfo.project.tenant_business" :readonly="true" />
                            </FormItem>
                        </Col>
                        <Col span="8" class="col_left">
                            <FormItem label="项目类型：">
                                <Input v-model="applyinfo.project.projectType" :readonly="true" />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row >
                        <Col span="8" >
                            <FormItem label="项目经理：">
                                <Input v-model="applyinfo.project.manager" :readonly="true" />
                            </FormItem>
                        </Col>
                        <Col span="8" class="col_left">
                            <FormItem label="经理联系方式:">
                                <Input v-model="applyinfo.project.manager_phone" :readonly="true"/>
                            </FormItem>
                        </Col>
                    </Row>
                </Form>
            </div>
        </div>
    </div>
</template>
<script>
    import ajax from "../../../ajax_util"
    import personBase from '../person_baseInfo.vue'
    export default {
		components:{
			personBase
		},
		data(){
            const validateReqSubject = (rule, value, callback) => {
                if (this.applyinfo.reqSubject === '') {
                    callback(new Error('填写工单标题'));
                }else{
                  if(this.applyinfo.reqSubject.length > 50){
                    callback(new Error('最多填写50个字符'));
                  }else{
                    callback()
                  }
                }
            };
            const validateRoleId = (rule, value, callback) => {
                if (this.applyinfo.roleId === undefined || this.applyinfo.roleId === null || this.applyinfo.roleId < 1) {
                    callback(new Error('请选择租户角色'));
                }else{
                    callback()
                }
            };
            const validateProjectId = (rule, value, callback) => {
                if (this.applyinfo.projectId === undefined || this.applyinfo.projectId === null || this.applyinfo.projectId < 1) {
                    callback(new Error('请选择关联项目'));
                }else{
                    callback()
                }
            };
            return {
                ruleLine:{
                    reqSubject:[
                        {required: true,validator: validateReqSubject, trigger: 'blur'}
                    ],
                    roleId: [
                        {required: true,type: 'number', message: '请选择租户角色', trigger: 'change',validator: validateRoleId},
                    ],
                    projectId: [
                        {required: true, type: 'number',message: '请选择关联项目', trigger: 'change',validator: validateProjectId},
                    ]
                },
                isPartShrink:[
                    {
                        value:0,
                        label:"租户销户"
                    },
                    {
                        value:1,
                        label:"资源/工具缩容"
                    }
                ],
                ifDrafts:false,
                tenantList:[],
                projectList:[],
            }
        },
        created:function(){
            console.log("-------");
        },
        mounted:function(){
            console.log('88888888888',this.reqId,this.draftsId);
            if(!this.isStep6){
                if (this.reqId == null && this.draftsId == null) {
                    this.getUserInfo();
                } else if (this.reqId != null) {
                    console.log("----baseinfo----------",this.applyinfo.user);
                    //this.getProject(this.applyinfo.user.id);
                } else if (this.draftsId != null) {
                    //this.getUserInfo();
                }
            }
        	console.log("--ww---->",this.applyinfo,this.reqId,this.draftsId);
        },
        methods: {
            validate(){
                var isSubmit = false
                this.$refs["projectInfo"].validate((valid) => {
                    console.log("0000000000",this.applyinfo.roleId,this.applyinfo.projectId);
                    if (valid) {
                        if(this.applyinfo.roleId == undefined || this.applyinfo.roleId < 1 || this.applyinfo.projectId == undefined || this.applyinfo.projectId < 1){
                            this.$Message.error('请填写基本信息');
                        }else{
                            isSubmit = true
                        }
                    } else {
                        this.$Message.error('请填写基本信息');
                    }
                })


                return isSubmit
            },
            // changeType(data){
            //     console.log("changeType",this.$parent.stepTitleId);
            // },
            getUserInfo(){
                this.applyinfo.user = this.$store.state.user;
                console.log("==user===",this.applyinfo.user);
                this.getProject(this.applyinfo.user.id);
            },
            getProject(id){
                console.log("==getProject===",id);
                var url = this.CONFIG.url_know + "/getManager/" + id
                var that = this;
                ajax.get({
                    url: url,
                    that: that,
                    params: null,
                    success: function(data) {
                        console.log("==projetc===",data,that.applyinfo.roleId);
                        that.tenantList = data;

                        if(that.applyinfo.roleId != undefined && that.applyinfo.roleId != null && that.applyinfo.roleId>0){
                            that.checkRole(that.applyinfo.roleId);
                        }
                    }
                })
            },
            checkRole(roleId){
                // console.log("======checkRole=======");
                //this.applyinfo.projectId = 0;
                this.applyinfo.project = {};
                //this.applyinfo.roleId = roleId;
                //this.applyinfo.project.projectId = 0;
                //this.projectList = [];
                this.tenantList.some(item =>{
                    if(roleId == item.id){
                        //this.applyinfo.project.roleName = item.tenant_name
                        //this.applyinfo.role = item;
                        this.projectList = item.projects
                        return true
                    }
                })
                console.log("======checkRole=======",this.applyinfo.projectId);
                if(this.applyinfo.projectId != undefined && this.applyinfo.projectId != null && this.applyinfo.projectId>0){
                    this.setProject(this.applyinfo.projectId,1);
                }
            },
            setProject(id,number){
                console.log("=========ppppppp=======", id,number);
                this.projectList.some(item=>{
                    if(id == item.id){
                        console.log("=====item===",item);
                        //this.applyinfo.projectId = id;
                        this.applyinfo.project = item;
                        if(!this.isStep6){
                            if(!this.update){
                                this.getData(item.id,0);
                            }else{
                                if(number == 0){
                                    this.getData(item.id,1);
                                }else{
                                    this.getData(item.id,2);
                                }
                            }
                        }
                    }
                })
            },
            getData(id,number){
                var url = this.CONFIG.progress_url + "/allocation-shrink-condition/" + id
                var that = this;
                ajax.get({
                    url: url,
                    that: that,
                    params: null,
                    success: function(data) {
                        console.log("=====数据啊啊啊=======",JSON.stringify(data));
                        if(number == 0){
                            if(data.resources!=undefined && data.resources!=null && data.resources!=""){
                                data.resources.forEach(item=>{
                                    item.shrinkVal = 0;
                                })
                            }

                            if(data.severs!=undefined && data.severs!=null && data.severs!=""){
                                data.severs.forEach(item=>{
                                    item.shrinkVal = 0;
                                    item.des = "";
                                    item.check = false;
                                })
                            }

                            if(data.datas!=undefined && data.datas!=null && data.datas!=""){
                                data.datas.forEach(item=>{
                                    if(item.serviceId < 1){
                                        item.serviceId = item.id;
                                    }
                                })
                            }

                            that.applyinfo.resources = data.resources;
                            that.applyinfo.severs = data.severs;
                            that.applyinfo.tools = data.tools;
                            that.applyinfo.datas = data.datas;
                            that.setAllData();
                        }else if(number == 1){
                            if(data.resources!=undefined && data.resources!=null && data.resources!=""){
                                data.resources.forEach(item=>{
                                    item.shrinkVal = 0;
                                })
                            }

                            if(data.severs!=undefined && data.severs!=null && data.severs!=""){
                                data.severs.forEach(item=>{
                                    item.shrinkVal = 0;
                                    item.des = "";
                                    item.check = false;
                                })
                            }

                            if(data.datas!=undefined && data.datas!=null && data.datas!=""){
                                data.datas.forEach(item=>{
                                    if(item.serviceId < 1){
                                        item.serviceId = item.id;
                                    }
                                })
                            }

                            that.applyinfo.resources = data.resources;
                            that.applyinfo.severs = data.severs;
                            that.applyinfo.tools = data.tools;
                            that.applyinfo.datas = data.datas;
                        }else if(number == 2){
                            if(data.resources!=undefined && data.resources!=null && data.resources!=""){
                                data.resources.forEach(item=>{
                                    item.shrinkVal = 0;
                                })
                            }

                            if(data.severs!=undefined && data.severs!=null && data.severs!=""){
                                data.severs.forEach(item=>{
                                    item.shrinkVal = 0;
                                    item.des = "";
                                    item.check = false;
                                })
                            }

                            if(data.datas!=undefined && data.datas!=null && data.datas!=""){
                                data.datas.forEach(item=>{
                                    if(item.serviceId < 1){
                                        item.serviceId = item.id;
                                    }
                                })
                            }

                            that.applyinfo.resources = data.resources;
                            that.applyinfo.severs = data.severs;
                            that.applyinfo.tools = data.tools;
                            that.applyinfo.datas = data.datas;
                            that.setUpdateData();
                        }
                    }
                })
            },
            setAllData(){
                var data = this.selectData
                console.log("kkkkkkkkkk",this.applyinfo,data);

                if(data.resources != undefined && data.resources != null && data.resources.length>0){
                    for(var i=0; i<this.applyinfo.resources.length; i++){
                        data.resources.some(item=>{
                            if(this.applyinfo.resources[i].serviceId == item.serviceId){
                                var json = JSON.stringify(item);
                                var temp = JSON.parse(json);
                                this.applyinfo.resources[i] = temp;
                            }
                        })
                    }
                }

                if(data.tools != undefined && data.tools != null && data.tools.length>0){
                    for(var i=0; i<this.applyinfo.tools.length; i++){
                        data.tools.some(item=>{
                            if(this.applyinfo.tools[i].toolId == item.serviceId){
                                var json = JSON.stringify(item);
                                var temp = JSON.parse(json);
                                this.applyinfo.tools[i] = temp;
                            }
                        })
                    }
                }

                if(data.servers != undefined && data.servers != null && data.servers.length>0){
                    for(var i=0; i<this.applyinfo.severs.length; i++){
                        data.servers.some(item=>{
                            if(this.applyinfo.severs[i].severId == item.serviceId){
                                var json = JSON.stringify(item);
                                var temp = JSON.parse(json);
                                this.applyinfo.severs[i] = temp;
                                //console.log("11111111111111111111",item);
                            }
                        })
                    }
                }

                if(data.datas != undefined && data.datas != null && data.datas.length>0){
                    for(var i=0; i<this.applyinfo.datas.length; i++){
                        data.datas.some(item=>{
                            if(this.applyinfo.datas[i].serviceId == item.serviceId){
                                var json = JSON.stringify(item);
                                var temp = JSON.parse(json);
                                this.applyinfo.datas[i] = temp;
                            }
                        })
                    }
                }

                console.log("mmmmmmmmmmmmmmmmmmm",this.applyinfo,data);
            },
            setUpdateData(){
                console.log("3333333333",this.applyinfo,this.detailData);
                var data = this.detailData

                if(data.resources != undefined && data.resources != null && data.resources.length>0){
                    for(var i=0; i<this.applyinfo.resources.length; i++){
                        data.resources.some(item=>{
                            if(this.applyinfo.resources[i].serviceId == item.serviceId ){
                                this.applyinfo.resources[i].check = true;
                                this.applyinfo.resources[i].shrinkVal = typeof item.shrinkVal == 'number'? item.shrinkVal:parseInt(item.shrinkVal);
                                return true;
                            }
                        })
                    }
                }

                if(data.tools != undefined && data.tools != null && data.tools.length>0){
                    for(var i=0; i<this.applyinfo.tools.length; i++){
                        data.tools.some(item=>{
                            if(this.applyinfo.tools[i].toolId == item.serviceId){
                                this.applyinfo.tools[i].check = true;
                                //this.applyinfo.tools[i].quantity = item.shrinkVal;
                                return true;
                            }
                        })
                    }
                }

                if(data.servers != undefined && data.servers != null && data.servers.length>0){
                    for(var i=0; i<this.applyinfo.severs.length; i++){
                        data.servers.some(item=>{
                            if(this.applyinfo.severs[i].severId == item.serviceId && this.applyinfo.severs[i].id == item.alloId){
                                this.applyinfo.severs[i].check = true;
                                this.applyinfo.severs[i].shrinkVal = item.shrinkVal;
                                console.log("======dddd=====",this.applyinfo.severs[i].quantitys);
                                this.applyinfo.severs[i].des = item.des;
                                return true;
                            }else{
                                this.applyinfo.severs[i].shrinkVal = 0;
                            }
                        })
                    }
                }

                if(data.datas != undefined && data.datas != null && data.datas.length>0){
                    for(var i=0; i<this.applyinfo.datas.length; i++){
                        data.datas.some(item=>{
                            if(this.applyinfo.datas[i].serviceId == item.serviceId){
                                this.applyinfo.datas[i].check = true;
                                return true;
                            }
                        })
                    }
                }

                console.log("zzzzzzzzzzzzzzzz",this.applyinfo,data);
            }
        },
        computed: {
            applyinfo: {
                get: function() {
                    return this.value;
                },
                set: function(value) {
                    this.$emit('input', value);
                }
            },
        },
        props:{
            reqId: {
                default: null
            },
            draftsId: {
                default: null
            },
            value: {},
            ifedit: {
                type: Boolean,
                default: false,
            },
            // tenantList:{
            //     default:[]
            // },
            // projectList:[],:{
            //     default:[]
            // },
            detailData:{
                default:[]
            },
            selectData:{
                default:[]
            },
            update: {
                type: Boolean,
                default: false,
            },
            isStep6: {
                type: Boolean,
                default: false
            }
        },
        watch:{
        	// tenantList(value){
        	// 	this.tenantList = value
        	// },
         //    projectList(value){
         //        this.projectList = value
         //    },
            detailData(value){
                this.detailData = value
            },
            selectData(value){
                this.selectData = value
            }
        }
    }
</script>
